﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using Assisticant;
using Assisticant.Collections;
using Assisticant.Fields;

namespace UGD.Models
{
    public class Document
    {
        private ObservableList<Item> _items = new ObservableList<Item>();
        private Observable<Item> _itemHeader = new Observable<Item>();
        private Observable<TimeSpan> _timespan = new Observable<TimeSpan>(new TimeSpan(0, 15, 0));
        private Observable<int> _pixels = new Observable<int>(10);
        private Observable<string> _filename = new Observable<string>();
        private Observable<string> _error = new Observable<string>("Hello");
     

        public Document(IEnumerable<Item> vehicles)
        {
            if (vehicles.Any())
            {
                _items.AddRange(vehicles);
            }

            if (_items.Count > 0)
            {
                _itemHeader.Value = Layout();
            }

        }

        public Item Layout()
        {
            var dumb = new Item();
            dumb.Name = "Name";
            dumb.ShowChart = false;
            double diff = (RangeEnd.AddHours(24) - RangeStart).TotalHours;
            DateTime dt = RangeStart;

            for (int i = 0; i < diff; i++)
            {
                if (i % 4 == 0)
                {
                    dumb.AddDuty(new Duty(dt, 4));
                }

                dt = dt.AddHours(1);
            }

            return dumb;
        }




        public DateTime RangeStart
        {
            get
            {
                var lowest = DateTime.Today;
                foreach (var item in _items)
                {
                    if (item.RangeStart < lowest.Date)
                    {
                        lowest = item.RangeStart;
                    }
                }
                return lowest.Date;
            }


        }

        public DateTime RangeEnd
        {
            get
            {
                DateTime highest = new DateTime(1, 1, 1, 1, 1, 1, 1);
                foreach (var item in _items)
                {
                    if (item.RangeEnd > highest.Date)
                    {
                        highest = item.RangeEnd;
                    }
                }
                return highest.Date;
            }
        }




        public IEnumerable<Item> Items => _items;

        public TimeSpan TimeSpan
        {
            get
            {
                return _timespan.Value;
            }
            set
            {
                _timespan.Value = value;
            }
        }
        public int Pixels
        {
            get
            {
                return _pixels.Value;
            }
            set
            {
                _pixels.Value = value;
            }
        }

        public string FileName
        {
            get
            {
                return _filename.Value;
            }
            set
            {
                _filename.Value = value;
            }
        }
        public string Error
        {
            get
            {
                return _error.Value;
            }
            set
            {
                _error.Value = value;
            }
        }


        public void LoadDataFile()
        {
         
                _items.Clear();
                csvReader helper = new csvReader(_filename.Value);
                _items.AddRange(helper.Read());
                _itemHeader.Value = Layout();
       }

        public Item ItemHeader
        {
            get
            {
                return _itemHeader.Value;
            }
            set
            {
                _itemHeader.Value = value;
            }
        }

    

    }
}



